package com.amikomgamedev.labirinhantu.state;

import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ScrollDetector;

import com.amikomgamedev.labirinhantu.Utils;


import android.view.KeyEvent;

public class State_Menu_Credits extends State_Menu_Level
{
	private boolean	manualScrolling;
	private float	y;

	protected void create_Menu_Credits()
	{
		m_Camera_Menu.initCamera(grafik.spr_Menu_Credits_Bg);
		m_Camera_Menu.centerCamera();
		isCameraMove = false;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
		if (State_Menu_Current == STATE_MENU_CREDITS)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
			{
				m_Camera_Menu.initCamera(grafik.spr_Menu_Main_Bg);

				isCameraMove = true;

				teks.setVisible(false);
				m_Hud.setY(0);

				return super.onKeyUp(keyCode, event);

			}
		}

		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		if (State_Menu_Current == STATE_MENU_CREDITS)
		{
			if (isCameraMove)
			{
				m_Camera_Menu.moveCamera(pSecondsElapsed);

				if (m_Camera_Menu.isStopCamera())
				{
					switchState(STATE_MENU_MAIN);
				}
			}

			y = m_Hud.getY();

			{
				if (!manualScrolling)
				{
					if (teks.year.getY() + teks.year.getHeight() > 0)
					{
						y -= Utils.getRatio(2);
						m_Hud.setPosition(m_Hud.getScaleCenterX() / 2, y);
					}

					if (m_Hud.getY() < -teks.year.getY())
					{
						m_Hud.setPosition(m_Hud.getScaleCenterX() / 2, 0);
					}

				}
			}

		}
		super.onUpdate(pSecondsElapsed);
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
	    if (State_Menu_Current == STATE_MENU_CREDITS)
	    {
		if (pSceneTouchEvent.isActionUp())
		{
			manualScrolling = false;
		} else

		{
			manualScrolling = true;
			m_Scroll.onTouchEvent(pSceneTouchEvent);

		}
	    }
		return super.onSceneTouchEvent(pScene, pSceneTouchEvent);

	}

	@Override
	public void onScroll(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		// TODO Auto-generated method stub
		if (State_Menu_Current == STATE_MENU_CREDITS)
		{
			y = m_Hud.getY() + pDistanceY;
			m_Hud.setPosition(m_Hud.getX(), y);
		}

		super.onScroll(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	@Override
	public void onScrollFinished(
	    ScrollDetector pScollDetector,
	    int pPointerID,
	    float pDistanceX,
	    float pDistanceY)
	{
	// TODO Auto-generated method stub
	super.onScrollFinished(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_MENU_CREDITS)
		sound.stopMenuBgm();
	    
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    if(State_Menu_Current == STATE_MENU_CREDITS)
		sound.playMenuBgm();

	    super.onResumeGame();
	}

}
